package com.library.controller.admin;

import com.library.pojo.PageResult;
import com.library.pojo.Result;
import com.library.pojo.User;
import com.library.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController("adminUser")
@Slf4j
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping
    public Result save(@RequestBody User user){
        user.setStatus(1);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userService.add(user);
        return Result.success();
    }
    /**
     * 分页查询用户
     */
    @GetMapping("/page")
    public Result pageQuery(@RequestParam(defaultValue = "1")Integer page
            ,@RequestParam(defaultValue = "10")Integer pageSize,String name,Integer status){
        PageResult result = userService.pageQuery(page,pageSize,name,status);
        return Result.success(result);
    }
    /**
     * 启用禁用用户
     */
    @PostMapping("/status/{status}")
    public Result StartOrStopUser(@PathVariable Integer status,Long id){
        userService.startOrStop(status,id);
        return Result.success();
    }
    /**
     * 根据id查询用户
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id ){
        User user = userService.getById(id);
        return Result.success(user);
    }
    /**
     * 修改用户
     */
    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }
}
